unit notebook;

{$mode ObjFPC}{$H+}

interface

uses
  Classes, SysUtils, SQLDB, Forms, Controls, Graphics, Dialogs, StdCtrls,
  ComCtrls, ButtonPanel;

type

  { Tform_notebook }

  Tform_notebook = class(TForm)
    ButtonPanel1: TButtonPanel;
    cb_notebook: TComboBox;
    sql_query_select_notebook: TSQLQuery;
    tv_notebook: TTreeView;
    procedure CancelButtonClick(Sender: TObject);
    procedure cb_notebookClick(Sender: TObject);
    procedure cb_notebookDropDown(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure OKButtonClick(Sender: TObject);
    procedure tv_notebookClick(Sender: TObject);
    procedure tv_notebookExit(Sender: TObject);
  private
    procedure RefreshNotebook;
  public
    dest_notebook_id: integer;
  end;

var
  form_notebook: Tform_notebook;

implementation

{$R *.lfm}

{ Tform_notebook }

procedure Tform_notebook.RefreshNotebook;
var
  sql: string;
  ANode, ParentNode: TTreeNode;
begin
  sql := 'WITH RecursiveNotebook AS (' + ' SELECT id, parent, name ' +
    ' FROM notebook ' + ' WHERE parent IS NULL ' + ' UNION ALL ' +
    ' SELECT c.id, c.parent, c.name ' + ' FROM notebook c ' +
    ' INNER JOIN RecursiveNotebook rc ON c.parent = rc.id ' +
    ') SELECT * FROM RecursiveNotebook;';
  sql_query_select_notebook.Close;
  sql_query_select_notebook.SQL.Text := sql;
  sql_query_select_notebook.Open;


  tv_notebook.Items.Clear;
  while not sql_query_select_notebook.EOF do
  begin
    if sql_query_select_notebook.FieldByName('parent').IsNull then
      ParentNode := nil
    else
      ParentNode := tv_notebook.Items.FindNodeWithData(
        Pointer(sql_query_select_notebook.FieldByName('parent').AsInteger));

    ANode := tv_notebook.Items.AddChild(ParentNode,
      sql_query_select_notebook.FieldByName('name').AsString);
    ANode.Data := Pointer(sql_query_select_notebook.FieldByName('id').AsInteger);
    ANode.ImageIndex := 0;
    sql_query_select_notebook.Next;
  end;
end;

procedure Tform_notebook.FormShow(Sender: TObject);
begin
  dest_notebook_id := 0;
  self.RefreshNotebook;
  tv_notebook.Visible := False;
  //notebook_filter_edit.FilteredTreeview := tv_notebook;
end;


procedure Tform_notebook.tv_notebookClick(Sender: TObject);
begin
  if tv_notebook.Selected <> nil then
  begin
    cb_notebook.Text := tv_notebook.Selected.Text;
    dest_notebook_id := integer(tv_notebook.Selected.Data);
    tv_notebook.Visible := False;
  end;
end;

procedure Tform_notebook.tv_notebookExit(Sender: TObject);
begin
  tv_notebook.Visible := False;
end;

procedure Tform_notebook.cb_notebookDropDown(Sender: TObject);
begin
  tv_notebook.SetBounds(cb_notebook.Left, cb_notebook.Top,
    cb_notebook.Width, self.Height - cb_notebook.Top - cb_notebook.Height);
  tv_notebook.Visible := True;
  tv_notebook.SetFocus;
end;

procedure Tform_notebook.FormCreate(Sender: TObject);
begin

end;

procedure Tform_notebook.cb_notebookClick(Sender: TObject);
begin
end;

procedure Tform_notebook.OKButtonClick(Sender: TObject);
begin
  ModalResult := mrOk;
  //self.Close;
end;

procedure Tform_notebook.CancelButtonClick(Sender: TObject);
begin
  ModalResult := mrCancel;
  //self.Close;
end;

end.
